package com.atjava.webspider.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpUtil {

	public static boolean find(String src,String pattern){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(src);
		if(m.find()){
			return true;
		}
		return false;
	}
	
	public static int findMatches(String src,String pattern){
		int n = 0;
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(src);
		while(m.find()){
			n++;
		}
		return n;
	}
	
	public static String replaceAll(String src,String to,String pattern,int groupnum){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(src);
		while(m.find()){
			if(m.groupCount() >= groupnum){
				src = replaceAll(src, m.group(groupnum), to) ;
			}
		}
		return src;
	}
	
	public static String replaceAll(String s,String target,String replace){
		if("".equals(target) && "".equals(replace))
			return s;
		while((s.indexOf(target)) >= 0){
			s = s.replace(target, replace);
		}
		return s;
	}
	
	public static String getGroup(String src,String pattern,int groupnum){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(src);
		if(m.find()){
			if(m.groupCount() >= groupnum){
				return m.group(groupnum);
			}
			return "";
		}
		return "";
	}
	
	public static List<String> getGroupList(String src,String pattern,int groupnum){
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(src);
		List<String> strs = new ArrayList<String>();
		while(m.find()){
			if(m.groupCount() >= groupnum){
				strs.add(m.group(groupnum));
			}
		}
		return strs;
	}
	
	public static String replaceBlank(String s){
		if(s == null)
			return null;
		return s.trim().replaceAll("\\n", "").replaceAll("\\t", "");
	}
	
	public static void main(String[] args) {
		/*String src = "<IMg   \r\n SRc  =    '   http://www.baidu.com   '  \r\n>";
		String src1 = "<iMG sRC = 'http://www.baidu.com/1.gif' >";
		String src2 = "<iMG src='xcv' style='http://www.baidu.com/1.gif' title='sdf' title=\"1321\"/>";
		String style = "<linK src='xcv' hreF=\"http://img1.cache.netease.com/img09/index/theme_blue1002.css\"  />";
		System.out.println(src1);*/
		//System.out.println("==="+getGroup(src2, "<(?i)linK[^>]*src\\s*=\\s*['\"]?\\s*([^\\s'\"^>]*)\\s*['\"]?[^>]*/?>", 1));
		String p = "<a href='http://develop.csai.cn/category.asp\\?class=.+'>(.+)</a> &gt; 正文";
		String s = "<a href='http://develop.csai.cn/category.asp?class=Java_Spring'>Spring</a> &gt; 正文";
		System.out.println(getGroup(s, p, 1));
	}
	
}
